package com.whut.v_model;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Range;
import com.whut.utils.RangeHelper;

import java.text.DecimalFormat;
import java.util.List;

public class MeasureResultData{

    /**
     * groups : [{"groupData":[{"observedValue":12},{"observedValue":1},{"observedValue":13},{"observedValue":2},{"observedValue":4}],"designValue":0},{"groupData":[{"observedValue":23},{"observedValue":23},{"observedValue":1},{"observedValue":3},{"observedValue":4}],"designValue":0}]
     * checkPart :
     * groupSize : 5
     * totalPointNum : 10
     */

    private String checkPart;
    private int groupSize;
    private int totalPointNum;
    private List<PointGroup> groups;

    public String getCheckPart() {
        return checkPart;
    }

    public void setCheckPart(String checkPart) {
        this.checkPart = checkPart;
    }

    public int getGroupSize() {
        return groupSize;
    }

    public void setGroupSize(int groupSize) {
        this.groupSize = groupSize;
    }

    public int getTotalPointNum() {
        return totalPointNum;
    }

    public void setTotalPointNum(int totalPointNum) {
        this.totalPointNum = totalPointNum;
    }

    public List<PointGroup> getGroups() {
        return groups;
    }

    public void setGroups(List<PointGroup> groups) {
        this.groups = groups;
    }


    /**
    * 最一般的生成
    *@author fuzhenjie
    *@date 19.9.18 20:22(GMT)
    *@param rangeMath 区间范围数学表达式String
    *@return JSONObject
    */
    public JSONObject generateRate(String rangeMath){
        //创建一个返回给调用者的jsonobj
        JSONObject rateJSON = new JSONObject();
        int totalPointsNum = 0,qualifiedPointsNum = 0;
        Range<Float> range = RangeHelper.getInstance(rangeMath);
        int count = 0;
        for (PointGroup pointGroup:this.groups) {
            for (Point point:pointGroup.getGroupData()) {
                float offset = point.getObservedValue()-pointGroup.getDesignValue();
                if (range.contains(offset)){
                    count++;
                }
                float round_val = (float)(Math.round(offset*100)/100);
                point.setOffset(round_val);
            }
        }
        float rate = (float)count /this.totalPointNum;

        //返回给调用者的数据赋值；
        totalPointsNum = this.totalPointNum;
        qualifiedPointsNum = count;
        rateJSON.put("singleRate",new DecimalFormat("#.##%").format(rate));
        rateJSON.put("totalPointsNum",totalPointsNum);
        rateJSON.put("qualifiedPointsNum",qualifiedPointsNum);

        return rateJSON;
    }

    /**
    * 一组为5个测量点，分别对应五个偏差值，测量值最小的作为设计值，每个实则值减去这个最小值作为偏差值
    *@author fuzhenjie
    *@date 19.9.18 20:16(GMT)
    *@param rangeMath 区间范围数学表达式String
    *@return JSONObject
    */
    public JSONObject generateRateType1(String rangeMath){
        //创建一个返回给调用者的jsonobj
        JSONObject rateJSON = new JSONObject();
        int totalPointsNum = 0,qualifiedPointsNum = 0;
        Range<Float> range = RangeHelper.getInstance(rangeMath);
        int count = 0;
        for (PointGroup pointGroup:this.groups) {
            float minAsDesignValue = Float.MAX_VALUE;
            for (Point point:pointGroup.getGroupData()) {
                minAsDesignValue = Math.min(minAsDesignValue, point.getObservedValue());
            }
            pointGroup.setDesignValue(minAsDesignValue);
            for (Point point:pointGroup.getGroupData()) {
                float offset = point.getObservedValue()-pointGroup.getDesignValue();
                if (range.contains(offset)){
                    count++;
                }
                float round_val = (float)(Math.round(offset*100)/100);
                point.setOffset(round_val);
            }
        }
        float rate = (float)count /this.totalPointNum;

        //返回给调用者的数据赋值；
        totalPointsNum = this.totalPointNum;
        qualifiedPointsNum = count;
        rateJSON.put("singleRate",new DecimalFormat("#.##%").format(rate));
        rateJSON.put("totalPointsNum",totalPointsNum);
        rateJSON.put("qualifiedPointsNum",qualifiedPointsNum);

        return rateJSON;
    }


    /**
    * 一组为2个或3个测量点，对应一个设计值和一个偏差值，偏差值为2个或3个测量数据分别减设计值，取其中偏差较大值作为误差值
    *@author fuzhenjie
    *@date 19.9.18 20:25(GMT)
    *@param rangeMath 区间范围数学表达式String
    *@return JSONObject
    */
    public JSONObject generateRateType2(String rangeMath){
        //创建一个返回给调用者的jsonobj
        JSONObject rateJSON = new JSONObject();
        int totalPointsNum = 0,qualifiedPointsNum = 0;

        Range<Float> range = RangeHelper.getInstance(rangeMath);
        //组合格率
        int countGroup = 0;
        for (PointGroup pointGroup:this.groups) {
            float maxOffset = 0.0f;
            for (Point point:pointGroup.getGroupData()) {
                float offset = point.getObservedValue()-pointGroup.getDesignValue();
                if(Math.abs(offset)>Math.abs(maxOffset)){
                    maxOffset = offset;
                }
               /* if (range.contains(offset)){
                    count++;
                }*/
                float round_val = (float)(Math.round(offset*100)/100);
                point.setOffset(round_val);
            }
            pointGroup.setMaxOffset(maxOffset);
            if (range.contains(maxOffset)){
                countGroup++;
            }
        }
        float rate = (float)countGroup /this.groups.size();
        //返回给调用者的数据赋值；
        totalPointsNum = this.groups.size();
        qualifiedPointsNum = countGroup;
        rateJSON.put("singleRate",new DecimalFormat("#.##%").format(rate));
        rateJSON.put("totalPointsNum",totalPointsNum);
        rateJSON.put("qualifiedPointsNum",qualifiedPointsNum);

        return rateJSON;
    }
}